<!--
PAGE DOCUMENTATION:
==================
This is a library containig all loging functions for the application.
See inline documentation for specific functions.
-->

<?php

	/*
	* @ params: 	$errorDecription - a string that describes the error.
	* @ purpose:	function prints a facebook error message to inform the user
	*				that an error occured.
	*/
	function printErrorMsg($errorDecription)
	{
		echo "<fb:error>
      		<fb:message>Error message</fb:message>
      		An error occured while ".$errorDecription.".</br>
      		The error was logged, and we will try to fix it.
      		If this problem happens again, please let us know!</br>
      		Thank you for your patience.
 			</fb:error>";
	}
	
	/*
	* @ params: 	$user_id - the ID of the user that caused the error. 
	*				$currentPage - the page on which the error happened.
	*				$errorDecription - a string that describes the error.
	* @ purpose:	function writes to error log, if an error occured.
	* @ return:		TRUE for success, FALSE for failure.
	*/
	function documentError($user_id,$current_page,$errorDecription)
	{
		$errorFileName = "errorLog.txt";
		$error = " error: ";
		
		$file = fopen($errorFileName,"a");
		if ($file==0){
			// file not opened 
			return FALSE;
		} else {
			$time = strftime("date:%d.%m.%y,time:%X");
			$errorString = $time.$error.$errorDecription."\n";
			$bytesSent = fwrite($file,$errorString);
			if ($bytesSent < strlen($errorString)) {
				// Write not completed 
				return FALSE;
			}
			fclose($file);
			logAction($user_id,$current_page,$error.$errorDecription);
			return TRUE;
		}
	}
	
	/*
	* @ params: 	$uid - the ID of the user that caused the action.
	*				$currentPage - the page on which the action happened.
	*				$str - a string that is to be loged.
	* @ purpose:	function writes to log a line indicating an actions.
	* @ return:		TRUE for success, FALSE for failure.
	*/
	function logAction($uid,$currentPage,$str)
	{
		$logFileName = "log.txt";
		
		$file = fopen($logFileName,"a");
		if ($file==0){
			// file not opened
			documentError("logAction failed - file not opened");
			return FALSE;
		} else {
			$time = strftime("date:%d.%m.%y,time:%X");
			$logString = $time." - UID=".$uid." - at page ".$currentPage." - ".$str."\n";
			$bytesSent = fwrite($file,$logString);
			if ($bytesSent < strlen($logString)) {
				// Write not completed
				documentError("logAction failed - write not completed");
				return FALSE;
			}
			fclose($file);
			return TRUE;
		}
	}

	/*
	* @ params: 	$uid - user ID that caused the action.
	*				$currentPage - the page on which the action happend.
	* @ purpose:	function writes to log a line indication mysqli connection oppened:
	*				"date:27.01.08,time:19:15:22 - UID=599507696 - at page index - new mysqli connection successful"
	* @ return:		void.
	*/
	function logMysqliConnectionOpened($uid,$currentPage)
	{
		logAction($uid,$currentPage,"new mysqli connection successful");
	}
		
	/*
	* @ params: 	$uid - user ID that caused the action.
	*				$currentPage - the page on which the action happend.
	* @ purpose:	function writes to log a line indication mysqli connection closed:
	*				"date:27.01.08,time:19:15:22 - UID=599507696 - at page index - mysqli connection closed successfuly."
	* @ return:		void.
	*/
	function logMysqliConnectionClosed($uid,$currentPage)
	{
		logAction($uid,$currentPage,"mysqli connection closed successfuly");
	}
		
	/*
	* @ params: 	$uid - user ID that caused the action.
	*				$page - the page to which the user is redirected
	*						example: send.
	*				$currentPage - the current page.
	* @ purpose:	function writes to log a line to indicate a redirection:
	*				"date:27.01.08,time:19:15:43 - UID=599507696 - at page sendMessage - redirected to send.php?new=0&recipient=0&index=58"
	* @ return:		void.
	*/
	function logRedirection($uid,$page,$currentPage)
	{
		logAction($uid,$currentPage,"redirected to $page");
	}
		
	/*
	* @ params: 	$uid - user ID that caused the action.
	*				$functionName - the name of the function called.
	*				$result - the returned valur of $functionName.
	*				$currentPage - the page to which the user was redirected
	*						example: send.
	* @ purpose:	function writes to log a line of function call:
	*				"date:27.01.08,time:19:16:43 - UID=599507696 - at page index - "countMessages" called. Arguments: (sqlDb,599507696,sent). Returned: 15"
	* @ return:		void.
	*/
	function logFunctionCall($uid, $functionName, $arguments, $result,$currentPage)
	{
		$str = "\"".$functionName."\" called. Arguments: (".$arguments."). Returned: ".$result;
		logAction($uid,$currentPage,$str);
	}
		
	/*
	* @ params: 	$uid - user ID that caused the action.
	*				$currentPage - the page to which the user was redirected
	*				$prevPage - the page in which the redirection happened.
	* @ purpose:	function writes to log the mysqli connection closed.
	* @ return:		void.
	*/
	/*	
	function logAfterRedirection($uid,$prevPage,$currentPage)
	{
		logAction($uid,$prevPage,"redirected to $currentPage.");
	}
	*/
?>